﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    /// <summary>
    /// 上下标式的辅助线装饰器。
    /// </summary>
    public class PaperSizeLineAdorner : Adorner
    {
        /// <summary>
        /// 用在主界面的标尺辅助线装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入对应编辑器的RootPanel。</param>
        public PaperSizeLineAdorner(Editor editor, Expressions.SubPanel adornedElement)
            : base(adornedElement)
        {
            masterEditor = editor;

            this.pageMargin = new Thickness(
                masterEditor.PageSettingInfo.MarginLeft,
                masterEditor.PageSettingInfo.MarginTop,
                masterEditor.PageSettingInfo.MarginRight,
                masterEditor.PageSettingInfo.MarginBottom
                );

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.MouseLeftButtonDown += new MouseButtonEventHandler(PaperSizeLineAdorner_MouseLeftButtonDown);

            this.Opacity = 50;
            this.ToolTip = "A4 估算线\r\n\r\n点击关闭";
        }

        void PaperSizeLineAdorner_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.IsEnable = false;
        }

        private bool isEnable = false;

        /// <summary>
        /// 是否启用此标尺。
        /// </summary>
        private bool IsEnable
        {
            get { return isEnable; }
            set
            {
                isEnable = value;
                this.InvalidateVisual();
            }
        }

        public void ShowPaperSizeLine(bool willShow)
        {
            IsEnable = willShow;
        }

        /// <summary>
        /// 如果已启用，返回真；否则返回假。
        /// </summary>
        /// <returns></returns>
        public bool GetShowing()
        {
            return isEnable;
        }

        private Editor masterEditor;

        public Editor MasterEditor
        {
            get { return masterEditor; }
        }
        
        private Orientation paperDirection = Orientation.Vertical;

        public Orientation PaperDirection
        {
            get { return paperDirection; }
            set
            {
                paperDirection = value;
                this.InvalidateVisual();
            }
        }

        private string paperName = "A4";

        public string PaperName
        {
            get { return paperName; }
            set
            {
                paperName = value;
                this.InvalidateVisual();
                this.ToolTip = paperName + " 估算线\r\n\r\n点击关闭";
            }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (isEnable == false) return;
            if (this.masterEditor == null) return;

            PaperInfo curPaperInfo = PaperInfo.GetPaperInfo(this.paperName);
            if (curPaperInfo == null) return;

            Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush = this.masterEditor.SubPanelSelectionAdornerBrush;

            Pen renderPen;

            renderPen = new Pen(renderBrush, 10)
            {
                EndLineCap = PenLineCap.Square,
                DashStyle = DashStyles.DashDotDot,
            };

            double x, y;
            switch (this.paperDirection)
            {
                case Orientation.Horizontal:
                    {
                        x = curPaperInfo.LongSideWPFUnit; y = curPaperInfo.ShortSideWPFUnit; break;
                    }
                default:
                    {
                        x = curPaperInfo.ShortSideWPFUnit; y = curPaperInfo.LongSideWPFUnit; break;
                    }
            }

            

            //再考虑页边距
            //x -= (masterEditor.PageSettingInfo.MarginLeft + masterEditor.PageSettingInfo.MarginRight);
            //y -= (masterEditor.PageSettingInfo.MarginTop + masterEditor.PageSettingInfo.MarginBottom);
            x -= (pageMargin.Left + pageMargin.Right);
            y -= (pageMargin.Top + pageMargin.Bottom);

            double realWidth = x;
            double realHeight = y;

            while (x < rect.Width)
            {
                drawingContext.DrawLine(renderPen,
                    new Point(x, 0),
                    new Point(x, rect.Height)
                    );
                x += realWidth;
            }

            while (y < rect.Height)
            {
                drawingContext.DrawLine(renderPen,
                      new Point(0, y),
                      new Point(rect.Width, y)
                      );
                y += realHeight;
            }

            //Point ptLeftTop = new Point(0, 0);
            //Point ptRightBottom;

            //switch (this.paperDirection)
            //{
            //    case Orientation.Horizontal:
            //        {
            //            ptRightBottom = new Point(curPaperInfo.LongLine, curPaperInfo.ShortLine); break;
            //        }
            //    default:
            //        {
            //            ptRightBottom = new Point(curPaperInfo.ShortLine, curPaperInfo.LongLine); break;
            //        }
            //}

            //Point ptRightTop = new Point(ptRightBottom.X, ptLeftTop.Y);
            //Point ptLeftBottom = new Point(ptLeftTop.X, ptRightBottom.Y);

            //PathGeometry pathGeometry = new PathGeometry();
            //PathFigure pf = new PathFigure() { StartPoint = ptRightTop };
            //PolyLineSegment pls = new PolyLineSegment(
            //    new List<Point>() { ptRightTop, ptRightBottom, ptLeftBottom, }, true);
            //pf.Segments.Add(pls);
            //pathGeometry.Figures.Add(pf);

            //drawingContext.DrawGeometry(null, renderPen, pathGeometry);
        }

        /// <summary>
        /// 构造函数中应初始化。
        /// </summary>
        private Thickness pageMargin;

        public Thickness PageMargin
        {
            get { return pageMargin; }

            set
            {
                pageMargin = value;
            }
        }
    }
}